package controllers;

import controllers.product.ProductController;
import models.Task;
import play.data.Form;
import play.mvc.Controller;
import play.mvc.Result;
import views.html.home;

/**
 * @class Application
 * @author andris
 * 
 */
public class Application extends Controller {

	static Form<Task> taskForm = form(Task.class);
	
	
	public static Result index() {
	    
		return redirect(routes.Application.home(0));
		// return ok(views.html.index.render("Hello Rendszerfejlesztés!"));
	}
	public static Result home(int page) {
	    return ok(
	            home.render(
	                ProductController.page(4, page,0)
	            )
	        );
	}
	

	/**
	 * tasks
	 * 
	 * @return ok(views.html.tasks.render(Task.all(), taskForm));
	 */
	public static Result tasks() {
		// String user = session("currentUserEmail");
		// if (user != null) {
		return ok(views.html.tasks.render(Task.all(), taskForm));
		// } else {
		// return redirect(controllers.user.routes.UserController.logIn());
		// }
	}

	public static Result newTask() {
		Form<Task> filledForm = taskForm.bindFromRequest();
		if (filledForm.hasErrors()) {
			return badRequest(views.html.tasks.render(Task.all(), filledForm));
		} else {
			Task.create(filledForm.get());
			return redirect(routes.Application.tasks());
		}
	}

	public static Result deleteTask(Long id) {
		Task.delete(id);
		return redirect(routes.Application.tasks());
	}
}